package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏西西
 * Date: 2023-09-07
 * Time: 16:34
 */
class MyThreadPool1{
    //此处不涉及到“时间”，此处只有任务，直接使用Runnable即可
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    //n表示线程的数量
    public MyThreadPool1(int n){
        //在这里创建出线程
        for (int i = 0; i < n; i++) {
            //创建线程，循环了n次
            Thread t = new Thread(() ->{
                while (true){
                    try {
                        Runnable runnable =queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }
    }
    //注册任务给线程池
    public void submit(Runnable runnable){
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
public class ThreadD27 {
    public static void main(String[] args) {
        MyThreadPool1 pool = new MyThreadPool1(10);
        for (int i = 0; i < 1000; i++) {
            int n = i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello"+ n);
                }
            });
        }
    }
}
